package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootGp;
import org.footballmanager.pandafootball.system.domain.FootUserPerson;
import org.footballmanager.pandafootball.system.form.impl.FootGpSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootGpMapper;
import org.footballmanager.pandafootball.system.service.FootGpService;
import org.footballmanager.pandafootball.system.service.FootUserPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class FootGpServiceImpl extends ServiceImpl<FootGp> implements FootGpService {
    @Autowired
    private FootGpMapper footGpMapper;
	@Autowired
	private FootUserPersonService footUserPersonService;

	@Override
	@Transactional
	public int delete(Serializable id, HttpServletRequest request) {
		FootGp t = this.get(id);
		FootUserPerson fup = footUserPersonService.get(t.getFupId());
		if(null != fup){
			fup.setGpzt(0);
			footUserPersonService.updateNotNull(fup, request);
		}
		log.info("删除球员挂牌:" + t.toString());
		return super.delete(id, request);
	}

    @Override
	public List<FootGp> findGpNotSuccess() {
	    FootGpSearchForm footGpSearchForm = new FootGpSearchForm();
	    footGpSearchForm.setCj(false);
		return this.findall(footGpSearchForm);
	}

	@Override
	public void clear(HttpServletRequest request) {
		List<FootGp> list = this.getall();
		for(FootGp gp: list){
			FootUserPerson fup = footUserPersonService.get(gp.getFupId());
			if(null == fup || fup.getXycs() <= 0){
				this.delete(gp.getId(), request);
			}
		}
	}

	@Override
	public void deleteByQyidAndUserId(Long qyId, Long userId, HttpServletRequest request) {
		Example example = new Example(FootGp.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("qyId", qyId);
		criteria.andEqualTo("sellUserId", userId);
		List<FootGp> list = this.findall(example);
		if(list != null && !list.isEmpty()){
			for(FootGp gp: list){
				this.delete(gp.getId(), request);
			}
		}
	}

    /**
     * 翻页方法
     *
     * @param footGpSearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootGp> findall(FootGpSearchForm footGpSearchForm) {
        return footGpMapper.findall(footGpSearchForm);
    }

	/**
	 * 根据主键获取记录
	 * @param id 主键
	 * @return 记录
	 */
	@Override
	public FootGp get(Serializable id) {
		return footGpMapper.findById(id);
	}

    /**
     * 查询
     *
     * @param entityname       实体类名
     * @param map              参数
     * @param footGpSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootGpSearchForm footGpSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footGpSearchForm);
        Page<FootGp> page = PageHelper.startPage(footGpSearchForm.getPageIndex(), footGpSearchForm.getPageSize());
        List<FootGp> list = this.findall(footGpSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footGpSearchForm 条件
     * @param request          请求
     */
    @Override
    public void deleteAll(FootGpSearchForm footGpSearchForm, HttpServletRequest request) {
        if(footGpSearchForm.getSelectIds() != null){
            String[] result = footGpSearchForm.getSelectIds().split(Constants.COMMA);
            for(String id: result){
                this .delete(Long.parseLong(id), request);
            }
        }
    }
}
